package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.SysAsset;
import com.ruoyi.system.domain.vo.SysAssetScrapVo;
import com.ruoyi.system.mapper.SysAssetMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAssetScrapMapper;
import com.ruoyi.system.domain.SysAssetScrap;
import com.ruoyi.system.service.ISysAssetScrapService;
import org.springframework.util.CollectionUtils;

/**
 * 资产报废申请Service业务层处理
 * 
 * @author sxy
 * @date 2025-04-02
 */
@Service
public class SysAssetScrapServiceImpl implements ISysAssetScrapService 
{
    @Autowired
    private SysAssetScrapMapper sysAssetScrapMapper;
    @Autowired
    private SysAssetMapper sysAssetMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询资产报废申请
     * 
     * @param scrapId 资产报废申请主键
     * @return 资产报废申请
     */
    @Override
    public SysAssetScrapVo selectSysAssetScrapByScrapId(Long scrapId)
    {
        SysAssetScrap sysAssetScrap = sysAssetScrapMapper.selectSysAssetScrapByScrapId(scrapId);
        SysAssetScrapVo scrapVo = new SysAssetScrapVo();
        BeanUtils.copyProperties(sysAssetScrap, scrapVo);
        if(sysAssetScrap.getApplicantId() != null){
            scrapVo.setApplicant(sysUserMapper.selectUserById(sysAssetScrap.getApplicantId()).getUserName());
        }
        if(sysAssetScrap.getReviewerId() != null){
            scrapVo.setReviewer(sysUserMapper.selectUserById(sysAssetScrap.getReviewerId()).getUserName());
        }
        if(sysAssetScrap.getAssetId() != null){
            scrapVo.setAssetName(sysAssetMapper.selectSysAssetByAssetId(sysAssetScrap.getAssetId()).getAssetName());
            scrapVo.setAssetNo(sysAssetMapper.selectSysAssetByAssetId(sysAssetScrap.getAssetId()).getAssetNo());
        }
        return scrapVo;
    }

    /**
     * 查询资产报废申请列表
     * 
     * @param sysAssetScrap 资产报废申请
     * @return 资产报废申请
     */
    @Override
    public List<SysAssetScrapVo> selectSysAssetScrapList(SysAssetScrap sysAssetScrap)
    {
        List<SysAssetScrap> sysAssetScraps = sysAssetScrapMapper.selectSysAssetScrapList(sysAssetScrap);
        List<SysAssetScrapVo> rsp = new ArrayList<>();
        if(!CollectionUtils.isEmpty(sysAssetScraps)){
            for (SysAssetScrap scrap : sysAssetScraps) {
                SysAssetScrapVo scrapVo = new SysAssetScrapVo();
                BeanUtils.copyProperties(scrap, scrapVo);
                if(scrap.getApplicantId() != null){
                    scrapVo.setApplicant(sysUserMapper.selectUserById(scrap.getApplicantId()).getUserName());
                }
                if(scrap.getReviewerId() != null){
                    scrapVo.setReviewer(sysUserMapper.selectUserById(scrap.getReviewerId()).getUserName());
                }
                if(scrap.getAssetId() != null){
                    scrapVo.setAssetName(sysAssetMapper.selectSysAssetByAssetId(scrap.getAssetId()).getAssetName());
                    scrapVo.setAssetNo(sysAssetMapper.selectSysAssetByAssetId(scrap.getAssetId()).getAssetNo());
                }
                rsp.add(scrapVo);
            }
        }
        return rsp;
    }

    /**
     * 新增资产报废申请
     * 
     * @param sysAssetScrap 资产报废申请
     * @return 结果
     */
    @Override
    public int insertSysAssetScrap(SysAssetScrap sysAssetScrap)
    {
        sysAssetScrap.setCreateTime(DateUtils.getNowDate());
        //随机设置审核人
        List<Long> assetAdmin = sysUserMapper.selectRoleUser("asset_admin");
        sysAssetScrap.setReviewerId(assetAdmin.get(new Random().nextInt(assetAdmin.size())));
        //状态默认待审核
        sysAssetScrap.setReviewStatus("待审核");
        return sysAssetScrapMapper.insertSysAssetScrap(sysAssetScrap);
    }

    /**
     * 修改资产报废申请
     * 
     * @param sysAssetScrap 资产报废申请
     * @return 结果
     */
    @Override
    public int updateSysAssetScrap(SysAssetScrap sysAssetScrap)
    {
        int i = sysAssetScrapMapper.updateSysAssetScrap(sysAssetScrap);
        if("通过".equals(sysAssetScrap.getReviewStatus())){
            //修改资产状态为报废
            SysAsset asset = new SysAsset();
            asset.setAssetId(sysAssetScrapMapper.selectSysAssetScrapByScrapId(sysAssetScrap.getScrapId()).getAssetId());
            asset.setStatus("报废");
            sysAssetMapper.updateSysAsset(asset);
        }
        return i;
    }

    /**
     * 批量删除资产报废申请
     * 
     * @param scrapIds 需要删除的资产报废申请主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetScrapByScrapIds(Long[] scrapIds)
    {
        return sysAssetScrapMapper.deleteSysAssetScrapByScrapIds(scrapIds);
    }

    /**
     * 删除资产报废申请信息
     * 
     * @param scrapId 资产报废申请主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetScrapByScrapId(Long scrapId)
    {
        return sysAssetScrapMapper.deleteSysAssetScrapByScrapId(scrapId);
    }
}
